package org.xncj.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.entity.PageResult;
import org.entity.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xncj.CheckGroupService;
import org.xncj.CheckGroup;
import org.xncj.CheckItem;
import org.xncj.dao.CheckGroupDao;
import org.xncj.dao.CheckItemDao;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Resource
    CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void AddCheckGroup(Integer[] checkItemIds, CheckGroup checkGroup) {
        checkGroupDao.AddCheckGroup(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        HashMap<String, Integer> map = new HashMap<>();
        map.put("checkGroupId", checkGroupId);
        for (Integer checkItemId : checkItemIds) {
            map.put("checkItemId", checkItemId);
            checkGroupDao.setCheckgroupAndCheckItem(map);
        }
    }

    @Override
    public CheckGroup getCheckGroupWithItems(Integer id) {
        // 查询CheckGroup对象
        CheckGroup checkGroup = checkGroupDao.findById(id);
        if (checkGroup == null) {
            return null; // 如果CheckGroup对象不存在，返回null
        }

        // 查询关联的CheckItem对象列表
        List<CheckItem> checkItems = checkItemDao.findByGroupId(id);
        checkGroup.setCheckItems(checkItems);

        return checkGroup;
    }
    @Override
    public PageResult findgroup(QueryPageBean queryPageBean){
        if ("null".equals(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("null");
        }
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page = checkGroupDao.findgroup(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }
    @Override
    public void deleteByid(Integer checkGroupId) {
        checkGroupDao.deleteByid(checkGroupId); // 删除组表
        checkGroupDao.deleteCheckItemIdByCheckGroupByid(checkGroupId); // 删除中间表
    }

    @Override
    public Integer[] findByCheckGroup(Integer checkGroupid) {
        Integer[] checkItemIds = checkGroupDao.findByCheckGroup(checkGroupid);
        return checkItemIds;
    }

    @Override
    public void updateCheckGroup(Integer[] checkItemIds, CheckGroup checkGroup) {
        Integer checkgroupid = checkGroup.getId();
        checkGroupDao.deleteCheckItemIdByCheckGroupByid(checkgroupid);
        checkGroupDao.updateCheckGroup(checkGroup);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("checkGroupId", checkgroupid);
        for (Integer checkItemId : checkItemIds) {
            map.put("checkItemId", checkItemId);
            checkGroupDao.setCheckgroupAndCheckItem(map);
        }
    }
}
